package com.jiuyou.clientController.commission;

import com.jiuyou.clientController.task.ClientTaskController;
import com.ruoyi.attached.domain.Attached;
import com.ruoyi.attached.service.IAttachedService;
import com.ruoyi.attachedData.service.IAttachedDataService;
import com.ruoyi.commission.domain.Commission;
import com.ruoyi.commission.service.ICommissionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.report.domain.Report;
import com.ruoyi.report.service.IReportService;
import com.ruoyi.sample.domain.Sample;
import com.ruoyi.sample.service.ISampleService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.task.domain.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 委托Controller
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@RestController
@RequestMapping("/client/commission")
public class ClientCommissionController extends BaseController
{
    @Autowired
    private ICommissionService commissionService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISampleService sampleService;

    @Autowired
    private IAttachedService attachedService;

    @Autowired
    private IReportService reportService;

    @Autowired
    private IAttachedDataService attachedDataService;

    @Autowired
    private ClientTaskController taskController;

    /**
     * 查询委托列表(所有)
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(Commission commission)
    {
        List<Commission> list = commissionService.selectCommissionListAll(commission);
        return AjaxResult.success(list);
    }

    /**
     * 查询委托列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Commission commission)
    {

        startPage();
        List<Commission> list = commissionService.selectCommissionList(commission);

        return getDataTable(list);
    }

    /**
     * 导出委托列表
     */
    @Log(title = "委托", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Commission commission)
    {
        List<Commission> list = commissionService.selectCommissionList(commission);
        ExcelUtil<Commission> util = new ExcelUtil<Commission>(Commission.class);
        return util.exportExcel(list, "委托数据");
    }

    /**
     * 获取委托详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(commissionService.selectCommissionById(id));
    }

    /**
     * 新增委托
     */
    @Log(title = "委托", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Commission commission)
    {
        String gc_commission_code=configService.selectConfigByKey("gc_commission_code");
        commission.setTableCode(gc_commission_code);

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR); // 获取当前年份
        int month = calendar.get(Calendar.MONTH) + 1; // 获取当前月份（需要加1，因为月份从0开始）
        commissionService.insertCommission(commission);
        Long id = commission.getId();
        String number = "WT"+year+"-"+month+id;
        Commission commission2 = new Commission();
        commission2.setId(id);
        commission2.setNumber(number);
        commissionService.updateCommission(commission2);

        return AjaxResult.success(id);
    }

    /**
     * 修改委托
     */
    @Log(title = "委托", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Commission commission)
    {
        commission.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(commissionService.updateCommission(commission));
    }

    /**
     * 删除委托
     */
    @Log(title = "委托", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commissionService.logicDeleteCommission(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }


    /**
     * 修改受理
     */
    @Log(title = "委托", businessType = BusinessType.UPDATE)
    @PostMapping("/acceptance")
    public AjaxResult acceptance(@RequestBody Commission commission) throws ParseException {
        Integer is_res = commissionService.acceptance(commission);
        return toAjax(is_res);
    }

    //委托单完结
    @PostMapping("/updateDone")
    public AjaxResult updateDone(@RequestParam Long commissionId){
        if(commissionId == null){
            return AjaxResult.error("请传入commissionId参数");
        }
        String res = commissionService.updateDone(commissionId);
        if("委托单已完成".equals(res)){
            return AjaxResult.success(res);
        }else{
            return AjaxResult.error(res);
        }
    }
}
